package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.Teacher;
import cn.ztydata.teachingsystem.heaven.entity.TeacherTeam;
import cn.ztydata.teachingsystem.heaven.service.TeacherService;
import cn.ztydata.teachingsystem.heaven.service.TeacherTeamService;
import cn.ztydata.teachingsystem.heaven.web.dto.TeacherListDto;
import cn.ztydata.teachingsystem.heaven.web.dto.TeacherTeamDto;
import cn.ztydata.teachingsystem.util.StringCodeUtil;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultList;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.MessageFormat;
import java.util.List;

/**
 * 教师团队控制器类
 * Created by chenxin on 2014/12/13.
 */
@Controller
@RequestMapping("/teacherTeam")
public class TeacherTeamController extends BaseController {
    @Autowired
    private TeacherTeamService teacherTeamService;
    @Autowired
    private TeacherService teacherService;

    /**
     * 新增教师团队成员
     * @param teacherTeamDto 教师团队dto
     * @return json
     *
     * @author cx
     * @since 2014-12-13
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult saveMember(@Valid @RequestBody TeacherTeamDto teacherTeamDto) {
        //映射到实体类
        TeacherTeam teacherTeam = mapper.map(teacherTeamDto, TeacherTeam.class);

        try {
            teacherTeamService.saveMemeber(teacherTeam);
        } catch (ServiceException ex) {
            log.debug("新增教师团队成员失败");
            return new JsonResult(false, ex.getMessage().isEmpty() ? "设置失败" : ex.getMessage());
        }

        log.debug("新增教师团队成员成功");
        return new JsonResult(true, "设置成功");
    }

    /**
     * 移除教师团队成员
     * @param teacherId 教师ID
     * @param courseId 课程ID
     * @return json
     *
     * @author cx
     * @since 2014-12-13
     */
    @RequestMapping(value = "/remove/{teacherId}/{courseId}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult deleteMember(@PathVariable long teacherId, @PathVariable long courseId) {
        //校验参数
        Assert.isTrue(teacherId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "教师ID"));
        Assert.isTrue(courseId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "课程ID"));

        try {
            teacherTeamService.deleteMember(teacherId, courseId);
        } catch (ServiceException ex) {
            log.debug("remove member failed");
            return new JsonResult(false, ex.getMessage().isEmpty() ? "移除失败" : ex.getMessage());
        }

        log.debug("remove member success");
        return new JsonResult(true, "移除成功");
    }

    /**
     * 获取教师团队列表
     * @param courseId 课程ID
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return json
     *
     * @author cx
     * @since 2014-12-13
     */
    @RequestMapping(value = "/list/{courseId}/{pageNum}/{pageSize}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage<TeacherTeamDto> getMembers(@PathVariable long courseId, @PathVariable int pageNum, @PathVariable int pageSize) {
        //校验参数
        Assert.isTrue(courseId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "课程ID"));
        Assert.isTrue(pageNum > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));

        //获取教学团队列表
        Page<TeacherTeam> page = teacherTeamService.getList(courseId, pageNum, pageSize);
        //映射到dto类
        Page<TeacherTeamDto> result = new Page<>(page.getTotal(), pageSize, pageNum, mapper.mapList(page.getItems(), TeacherTeamDto.class));

        log.debug("return teacherteam page list");
        return new JsonResultPage<>(result);
    }

    /**
     * 获取教师列表
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @param keyword 搜索关键字
     * @param deptNum 系部编码
     * @return Page
     *
     * @author cx
     * @since 2014-12-15
     */
    @RequestMapping(value = "/teacherList/{pageNum}/{pageSize}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage<TeacherListDto> getTeacherList(@PathVariable int  pageNum, @PathVariable int pageSize,
                                                     @RequestParam(required = false) String keyword, @RequestParam(value = "depart", required = false) String deptNum) {
        //校验参数
        if ("null".equals(deptNum)) {
            log.debug("没有选择系部:null.equals(deptNum)");
            deptNum = null;
        }

        Assert.isTrue(pageNum > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));

        //搜索关键字转码
        keyword = StringCodeUtil.urlParamTransCode(keyword, "utf-8");

        //获取分页教师列表
        Page<Teacher> page = teacherService.getListForTeam(pageNum, pageSize, deptNum, StringUtils.trim(keyword));
        //映射到dto类
        Page<TeacherListDto> result = new Page<>(page.getTotal(), pageSize, pageNum, mapper.mapList(page.getItems(), TeacherListDto.class));

        log.debug("return teacher page list");
        return new JsonResultPage<>(result);
    }

    /**
     * 获取教师团队头像
     * @param courseId 课程ID
     * @return json
     *
     * @author cx
     * @since 2014-12-15
     */
    @RequestMapping(value = "/photo/{courseId}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultList<TeacherTeamDto> getTeamPhotoList(@PathVariable long courseId) {
        //校验参数
        Assert.isTrue(courseId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "课程ID"));

        //获取教师团队头像
        List<TeacherTeam> teacherTeamList = teacherTeamService.getListForPic(courseId);
        //映射到dto类
        List<TeacherTeamDto> result = mapper.mapList(teacherTeamList, TeacherTeamDto.class);

        log.debug("return member photoes");
        return new JsonResultList<>(result);
    }

}
